﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FlowItemsControl.cs" company="">
//   
// </copyright>
// <summary>
//   The flow items control.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    /// <summary>
    /// The flow items control.
    /// </summary>
    public partial class FlowItemsControl : ItemsControl
    {
        #region Constructors and Destructors
        /// <summary>
        /// Initializes a new instance of the <see cref="FlowItemsControl"/> class.
        /// </summary>
        public FlowItemsControl()
        {

            InitializeComponent();

				FlowPanel.Click += FlowPanel_Click;
        }

		  public void FlowPanel_Click(object sender, EventArgs e)
		  {
			  // To allow scroll of owner control
			  FlowPanel.Focus();
		  }
        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The add item view.
        /// </summary>
        /// <param name="typeName">
        /// The type name.
        /// </param>
        /// <param name="itemControl">
        /// The item control.
        /// </param>
        /// <returns>
        /// The <see cref="Control"/>.
        /// </returns>
        public override Control AddItemView(string typeName, Control itemControl)
        {
            FlowPanel.Controls.Add(itemControl);

				this.SizeChanged += FlowItemsControl_SizeChanged;
				this.Resize += FlowItemsControl_Resize;

				itemControl.Show();

            return itemControl;
        }

        /// <summary>
        /// The clear.
        /// </summary>
        public override void Clear()
        {
            FlowPanel.Controls.Clear();
        }

        /// <summary>
        /// The delete item view.
        /// </summary>
        /// <param name="view">
        /// The view.
        /// </param>
        public override void DeleteItemView(Control view)
        {
            FlowPanel.Controls.Remove(view);
        }

        public IEnumerable<Control> ItemViews
        {
            get
            {
                foreach (Control control in FlowPanel.Controls)
                {
                    yield return control;
                }
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The flow panel_ resize.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
		  private void FlowPanel_Resize(object sender, EventArgs e)
		  {
			  this.SuspendLayout();
			  AutoSize = false;
			  Height = FlowPanel.Size.Height + Padding.Top + Padding.Bottom;
			  Width = FlowPanel.Size.Width + Padding.Left + Padding.Right;
			  this.ResumeLayout(true);
		  }
		  private void FlowPanel_SizeChanged(object sender, EventArgs e)
		  {

		  }

		  void FlowItemsControl_Resize(object sender, EventArgs e)
		  {
			  dummyTableLayoutPanel.Height = Height - (Padding.Top + Padding.Bottom);
			  dummyTableLayoutPanel.Width = Width - (Padding.Left + Padding.Right);
		  }

		  void FlowItemsControl_SizeChanged(object sender, EventArgs e)
		  {
			  
		  }

        #endregion

        public FlowDirection FlowDirection
        {
            get
            {
                return FlowPanel.FlowDirection;
            }
            set
            {
                FlowPanel.FlowDirection = value;
            }
        }
    }
}